001 package videoautomat; 002 import java.awt.Rectangle; 003 import java.io.FileOutputStream; 004 import java.io.IOException; 005 import java.util.Iterator; 006 007 import log.Log; 008 import sale.Action; 009 import sale.CalendarTime; 010 import sale.MenuSheet; 011 import sale.MenuSheetItem; 012 import sale.SaleProcess; 013 import sale.SalesPoint; 014 import sale.Shop; 015 import sale.StepTimer; 016 import users.UserManager; 017 import data.QuoteValue; 018 import data.Value; 019 import data.events.VetoException; 020 import data.ooimpl.CatalogImpl; 021 import data.ooimpl.CountingStockImpl; 022 import data.ooimpl.EUROCurrencyImpl; 023 import data.ooimpl.MoneyBagImpl; 024 025 /** 026 * This is the central class for this application - the <code>Shop</code>. It contains the 027 * video-catalog and stock of this video-shop, the shop`s money and therefor a currency-catalog 028 * containing which coins this automat accepts and a <code>Timer</code> for simulation purposes. 029 * 030 */ 031 public class VideoShop extends Shop { 032 /** 033 * Key of the video -<code>Catalog</code> 034 */ 035 public static final String C_VIDEOS = "VideoCatalog"; 036 /** 037 * Key of the currency -<code>Catalog</code> 038 */ 039 public static final String C_CURRENCY = "CurrencyCatalog"; 040 /** 041 * Key of the video -<code>CountingStock</code> 042 */ 043 public static final String CC_VIDEOS = "VideoStock"; 044 /** 045 * Key of the <code>MoneyBag</code> 046 */ 047 public static final String MB_MONEY = "Money"; 048 /** 049 * The name of the global log file 050 */ 051 public static final String FILENAME = "automat.log"; 052 053 /** The Caption of the <code>VideoAutomat</code> */ 054 public static final String CAPTION_AUTOMAT = "****** VIDEOAUTOMAT *** 24 H ******"; 055 056 /** Label for the menusheet that contains self-defined items */ 057 public static final String MS_NEW = "Videoautomat"; 058 059 /** Label for the button to start the automat */ 060 public static final String MSI_AUTOMAT = "Start automat"; 061 062 /** 063 * Label for the admin- <code>ActionCapability</code>, if it`s not granted. 064 */ 065 public static final String MSG_ACCESS = "Acces denied!!!"; 066 /** 067 * Label for the time setter button 068 */ 069 public static final String MSI_DAY = "+ 1 Day"; 070 /** 071 * Constructs a new VideoShop and set it as <code>Shop#setTheShop()</code>. Also initialize 072 * the global <code>Catalogs</code> and <code>Stocks</code> and sets a <code>Timer</code>. 073 * 074 */ 075 public VideoShop() { 076 super(); 077 setShopFrameBounds(new Rectangle(0, 0, 640, 480)); 078 079 addCatalog(new CatalogImpl(C_VIDEOS)); 080 addCatalog(new EUROCurrencyImpl(C_CURRENCY)); 081 addStock( 082 new CountingStockImpl( 083 CC_VIDEOS, 084 (CatalogImpl) getCatalog(C_VIDEOS))); 085 086 UserManager.setGlobalUM(new UserManager()); 087 088 addStock( 089 new MoneyBagImpl( 090 MB_MONEY, 091 (EUROCurrencyImpl) getCatalog(C_CURRENCY))); 092 093 setTimer(new StepTimer(new CalendarTime(System.currentTimeMillis()))); 094 setShopFrameTitle(getTimer().getTime().toString()); 095 try { 096 Log.setGlobalOutputStream(new FileOutputStream(FILENAME, true)); 097 } catch (IOException ioex) { 098 System.err.println("Unable to create log file."); 099 } 100 } 101 102 /** 103 * @return the <code>Shop`s MenuSheet</code>, containing the default one, a button to start 104 * an automat and a button to switch the time further. 105 * @see sale.Shop#createShopMenuSheet() 106 */ 107 protected MenuSheet createShopMenuSheet() { 108 MenuSheet ms_default = super.createShopMenuSheet(); 109 MenuSheet ms_new = new MenuSheet(MS_NEW); 110 MenuSheetItem msi_automat = 111 new MenuSheetItem(MSI_AUTOMAT, new Action() { 112 public void doAction(SaleProcess p, SalesPoint sp) 113 throws Throwable { 114 addSalesPoint(new VideoAutomat()); 115 } 116 }); 117 ms_new.add(msi_automat); 118 MenuSheetItem msi_time = 119 new MenuSheetItem(MSI_DAY, new Action() { 120 public void doAction(SaleProcess p, SalesPoint sp) 121 throws Throwable { 122 getTimer().goAhead(); 123 setShopFrameTitle(getTimer().getTime().toString()); 124 } 125 }); 126 ms_new.add(msi_time); 127 ms_default.add(ms_new); 128 return ms_default; 129 } 130 /** 131 * Overidden to avoid the annoying save-query, when quiting the application. 132 * 133 * @see sale.Shop#quit() 134 */ 135 public void quit() { 136 if (shutdown(false)) { 137 System.exit(0); 138 } 139 } 140 /** 141 * Helper method to avoid to long code-lines. 142 * 143 * @return the global <code>Catalog</code> of videos. 144 */ 145 public static CatalogImpl getVideoCatalog() { 146 return (CatalogImpl) Shop.getTheShop().getCatalog(C_VIDEOS); 147 } 148 /** 149 * Helper method to avoid to long code-lines. 150 * 151 * @return the global <code>Stock</code> of videos 152 */ 153 public static CountingStockImpl getVideoStock() { 154 return (CountingStockImpl) Shop.getTheShop().getStock(CC_VIDEOS); 155 } 156 157 /** 158 * Helper method to avoid to long code-lines. 159 * 160 * @return the global <code>MoneyBag</code> containing the money of the shop. 161 */ 162 public static MoneyBagImpl getMoneyBag() { 163 return (MoneyBagImpl) Shop.getTheShop().getStock(MB_MONEY); 164 } 165 /** 166 * Helper method to avoid to long code-lines. 167 * 168 * @return the global <code>EUROCurrencyImpl</code> -instance 169 */ 170 public static EUROCurrencyImpl getCurrency() { 171 return (EUROCurrencyImpl) Shop.getTheShop().getCatalog(C_CURRENCY); 172 } 173 174 /** 175 * Method to iterate over all rented videos and taking out those, which renting costs exceed 176 * the purchase price 177 * 178 */ 179 public static void checkRentedVideos() { 180 Iterator it_users = UserManager.getGlobalUM().getUsers().iterator(); 181 while (it_users.hasNext()) { 182 AutomatUser c = (AutomatUser) it_users.next(); 183 Iterator it_stock = c.getVideoStock().iterator(null, false); 184 while (it_stock.hasNext()) { 185 VideoCassette vc = (VideoCassette) it_stock.next(); 186 Value v = 187 ((QuoteValue) vc.getAssociatedItem(null).getValue()) 188 .getOffer(); 189 if (vc.getCost().compareTo(v) >= 0) { 190 try { 191 c.getVideoStock().remove(vc, null); 192 } catch (VetoException e) { 193 e.printStackTrace(); 194 } 195 } 196 } 197 } 198 } 199 }